package com.util;

import com.pojo.Petinfo;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.UUID;

/**
 * @author Zhengjz
 * @version 1.0
 * @className UploadImg
 * @date 2021/3/1 22:30
 */
public class UploadImg {

    public Petinfo uploadImg(MultipartFile file,Petinfo petinfo,Integer userId) {
        String path = "D:\\IDEAcode\\Workidea\\pet-tuoguan\\src\\main\\resources\\static\\images\\petimg";
        String fileName = file.getOriginalFilename();
        String suffixName=fileName.substring(fileName.lastIndexOf("."));
        fileName= UUID.randomUUID()+suffixName;
        System.out.println(fileName);
        System.out.println("type::" + suffixName);
        System.out.println("filename::" + fileName);
        File targetFile = new File(path);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        File saveFile = new File(targetFile, fileName);
        try {
            file.transferTo(saveFile);
            System.out.println("执行成功");

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("执行失败");
        }
        //把图片加图片的路径写入数据库
       // System.out.println("图片："+path+fileName);
        String path1 = path + fileName;
        //  System.out.println(path1);
        petinfo.setImgUrl(fileName);
        petinfo.setDelFlag(0);
        petinfo.setPetmasterId(userId);
        System.out.println("petinfo:"+petinfo);
     return petinfo;
    }

}
